package cn.cvs.Controller;

import cn.cvs.pojo.SysUser;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

@Controller
@RequestMapping("/user")
public class SysController {
    private Logger logger = Logger.getLogger(SysController.class);
    private ArrayList<SysUser> sysUserList = new ArrayList<SysUser>();
    private ArrayList<SysUser> queryUserList = new ArrayList<SysUser>();
    private ArrayList<SysUser> userList = new ArrayList<SysUser>();
    public SysController(){
        try {
            sysUserList.add(new SysUser(5,"zhaojian","赵静","5555555",1
                ,"13054784445","上海市宝山区",1,1,new Date(),1,new Date()));
            sysUserList.add(new SysUser(4,"wanglin","王林","4444444",1
                    ,"18965622364","北京市学院路",1,1,new Date(),1,new Date()));
            sysUserList.add(new SysUser(1,"test001","测试用户001","1111111",1
                    ,"13054784445","北京市朝阳区",1,1,new Date(),1,new Date()));
            sysUserList.add(new SysUser(2,"zhaoyan","赵燕","2222222",1
                    ,"18678786545","北京市海淀区",1,1,new Date(),1,new Date()));
            sysUserList.add(new SysUser(3,"test003","测试用户003","1111111",1
                    ,"13121334531","北京市海淀区",1,1,new Date(),1,new Date()));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    //查询所有用户
    @GetMapping("/list")
    public String list(Model model){
        logger.info("当查询条件为空时，查询用户信息");
        queryUserList.clear();
        queryUserList.addAll(userList);
        model.addAttribute("queryUserList",queryUserList);
        return "sysUser/sysUserList";
    }

    //根据条件查询部分用户
    @PostMapping("/list")
    public String list(@RequestParam(value = "realName",required = false)String realName,Model model){
        logger.info("查询条件:【realName】= " + realName +",查询用户信息");
        queryUserList.clear();
        if (realName != null && !realName.equals("")){
            for (SysUser user:userList){
                if (user.getRealName().indexOf(realName) != -1){
                    queryUserList.add(user);
                }
            }
        }else {
            queryUserList.addAll(userList);
        }
        model.addAttribute("queryUserList",queryUserList);
        return "sysUser/sysUserList";
    }
}